package com.cy.impl;

import com.cy.pojo.User;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Component
public class NoticeJdbcTemplateImpl implements NoticeJdbcTemplateDao{

    private DataSource dataSource;

    private JdbcTemplate jdbcTemplate;
    public NoticeJdbcTemplateImpl(DataSource dataSource){
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public void update(String sql, Object[] notice) {
        jdbcTemplate.update(sql,notice);
    }

//    @Override
//    public List<Map<Integer, String>> select(String sql, Object[] values) {
//      //  jdbcTemplate.query(sql,values);
//        return null;
//    }

    //返回一个对象
    public void test(){
        User user = jdbcTemplate.queryForObject("select id,title from sys_notices where id =1", new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setTitle(rs.getString("title"));
                return user;
            }
        });

        System.out.println(user);
    }

    @Override
    public void selectList() {
        List<User> list = jdbcTemplate.query("select id,title from sys_notices", new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setTitle(rs.getString("title"));
                return user;
            }
        });
        System.out.println(list);
    }


}
